package com.my.concurrency.threadpool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  自定义线程工厂
 *
 * @author: marshal
 * @date: 2023-03-03 2:19 PM
 */
public class CustomThreadFactory implements ThreadFactory {

    private AtomicInteger threadId = new AtomicInteger();

    private String prefix;

    public CustomThreadFactory(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        // 自定义线程名
        thread.setName(prefix + "-xxxjob-" + threadId.getAndIncrement());
        return thread;
    }
}
